<?php

namespace App\Models\Clickhouse\Guanyuan;


use App\Models\Clickhouse\BaseCHModel;
use App\Service\Kms\Home\HomeService;
use Kuafu\LaravelClickHouse\Database\Eloquent\Builder;

class GyCityNewlyOpenedShopCHModel extends BaseCHModel
{
    protected $table = "gy_city_newly_opened_shop";

    /**
     * 首页查询条件构造
     *
     * @param int $regionSelectionType
     * @param string $region
     * @return Builder
     */
    public static function buildHomeSearchQuery(int $regionSelectionType, string $region): Builder
    {
        switch ($regionSelectionType) {
            case HomeService::REGION_SELECTION_TYPE_1:
                $query = function ($query) use ($region) {
                    return $query->where("region", "LIKE", "%" . str_replace("分公司", "", $region) . "%");
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_2:
                $query = function ($query) use ($region) {
                    return $query->where("province", $region);
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_3:
                $query = function ($query) use ($region) {
                    return $query->where("city", $region);
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_0:
            default:
                $query = null;
                break;
        }
        return self::query()->when($query, function($subQuery)use($query){return $subQuery->where($query);});
    }
}
